package com.gw.quotes.activity.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;

import com.gw.quotes.MyApplication;
import com.gw.quotes.activity.MainActivity;
import com.gw.quotes.entity.system.User;
import com.gw.quotes.util.ACache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.yxw.view.YXWProgressDialog;
import com.yxw.view.YXWProgressDialog.YXWDialog;

/**
 * 
 * @author yxw
 * @version 1.0.1 为了兼容改成fragment
 * 
 */
public class BaseMainFragment extends Fragment {

	// 容器
	protected MainActivity mActivity = null;
	protected Context context;
	protected YXWDialog dialog;
	protected ImageLoader imageLoader;
	protected DisplayImageOptions options;
	protected ACache aCache;

	// ==================友盟==========================
	// 首先在您的Activity中添加如下成员变量

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivity = (MainActivity) getActivity();
		context = getActivity().getApplicationContext();
		aCache = ACache.get(mActivity);
		dialog = new YXWProgressDialog(mActivity).createDialog("加载中…");
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(ImageLoaderConfiguration.createDefault(context));
		// options = new DisplayImageOptions.Builder()
		// .showImageOnLoading(R.drawable.gw_menu_header)
		// .showImageForEmptyUri(R.drawable.gw_menu_header)
		// .showImageOnFail(R.drawable.gw_menu_header).cacheInMemory(true)
		// .cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	protected MyApplication getMyApplication() {
		return MyApplication.instance();
	}
	
	protected User getLoginUser() {
		return MyApplication.instance().getLoginUser();
	}
	protected void startActivityAnimForResult(Intent intent, int requestCode,
			int enterAnim, int exitAnim) {
		startActivityForResult(intent, requestCode);
		getActivity().overridePendingTransition(enterAnim, exitAnim);
	}

	// protected void startActivityAnim(Intent intent) {
	// startActivity(intent);
	// getActivity().overridePendingTransition(R.anim.uc_alpha_in,
	// R.anim.uc_alpha_out);
	// }

	protected void startActivityAnim(Intent intent, int enterAnim, int exitAnim) {
		startActivity(intent);
		getActivity().overridePendingTransition(enterAnim, exitAnim);
	}

	/**
	 * MainActivity中的dispatchKeyEvent方法调用了BaseMainFragment 实现Fragment中监听返回键功能
	 * 
	 * @param event
	 * @return
	 */
	public boolean dispatchKeyEvent(KeyEvent event) {

		new AlertDialog.Builder(getActivity()).setTitle("提示")
				.setMessage("确认退出？")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						getActivity().finish();
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
					}
				}).show();
		return false;

	}
}
